package com.jeeplus.mob.common;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class UploadQiNiu {

	static final String accessKey = "HnYPMvzIIFWftKuQGfR5IF-guUT9wGHjP-svCAx8";
	static final String secretKey = "KyiOfcw0boKBMpzP8emkG8cWdEckk4nQ5DyAvcQd";
	static final String bucket = "zbspace";// 空间名称
	public static final String link = "http://pic.weisou.online/";// 绑定域名 测试使用http

	// 服务器文件直传
	public static String uploadFile(String filePath) {
		// 构造一个带指定 Region 对象的配置类
		Configuration cfg = new Configuration(Zone.zone0());
		// ...其他参数参考类注释
		UploadManager uploadManager = new UploadManager(cfg);
		// ...生成上传凭证，然后准备上传
		// 如果是Windows情况下，格式是 D:\\qiniu\\test.png
		String localFilePath = filePath;
		// 默认不指定key的情况下，以文件内容的hash值作为文件名
		String key = null;
		Auth auth = Auth.create(accessKey, secretKey);
		String upToken = auth.uploadToken(bucket);
		String showPath = "";
		try {
			Response response = uploadManager.put(localFilePath, key, upToken);
			// 解析上传成功的结果
			DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
			System.out.println(putRet.key);
			System.out.println(putRet.hash);
			showPath = putRet.key;
		} catch (QiniuException ex) {
			Response r = ex.response;
			System.err.println(r.toString());
			try {
				System.err.println(r.bodyString());
			} catch (QiniuException ex2) {
				// ignore
			}
		}
		return showPath;
	}

    // 获取七牛云token
    public static String getQiNiuToken() {
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);
        return upToken;
    }


	// 字节数组上传
	public static String byteUploadFile(byte[] uploadFileBytes) {
		// 构造一个带指定 Region 对象的配置类
//		Configuration cfg = new Configuration(Zone.zone0());
		Configuration cfg = new Configuration(Region.region0());
		// ...其他参数参考类注释
		UploadManager uploadManager = new UploadManager(cfg);
		// ...生成上传凭证，然后准备上传
		// 默认不指定key的情况下，以文件内容的hash值作为文件名
		String key = StringHelper.GetGUID();
		String showPath = "";
		byte[] uploadBytes = uploadFileBytes;
		Auth auth = Auth.create(accessKey, secretKey);
		String upToken = auth.uploadToken(bucket);
		try {
			Response response = uploadManager.put(uploadBytes, key, upToken);
			// 解析上传成功的结果
			DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
			System.out.println(putRet.key);
			System.out.println(putRet.hash);
			showPath = putRet.key;
		} catch (QiniuException ex) {
			Response r = ex.response;
			System.err.println(r.toString());
			try {
				System.err.println(r.bodyString());
			} catch (QiniuException ex2) {
				// ignore
			}
		}
		return link + showPath;
	}

	// 自己写一个FileUitl工具类
	public static byte[] readToByte(File file) throws IOException {
		long length = file.length();
		if (length > Integer.MAX_VALUE)
			throw new IOException("file too large");
		byte[] b = null;
		InputStream in = null;
		try {
			b = new byte[(int) length];
			in = new FileInputStream(file);
			int offset = 0, nRead = 0;
			while (offset < b.length && (nRead = in.read(b, offset, b.length - offset)) != -1)
				offset += nRead;
		} catch (Exception e) {

		} finally {
			try {
				if (in != null)
					in.close();
			} catch (IOException e) {

			}
		}
		return b;
	}

	public static void del(String key) {
		//构造一个带指定 Region 对象的配置类
		Configuration cfg = new Configuration(Zone.zone2());
		//...其他参数参考类注释
		Auth auth = Auth.create(accessKey, secretKey);
		BucketManager bucketManager = new BucketManager(auth, cfg);
		try {
		    bucketManager.delete(bucket, key);
		} catch (QiniuException ex) {
		    //如果遇到异常，说明删除失败
		    System.err.println(ex.code());
		    System.err.println(ex.response.toString());
		}
	}

	public static void main(String[] args) throws IOException {
		String path = "C:\\Users\\Think\\Pictures\\2ed05ea576a044a7bfb291d20081bfcb.jpg";
//		 String filepath = uploadFile(path);
//		 System.out.println(filepath);
		File file = new File(path);
//		System.out.println(file.length());
//		// File转byte[ ]
//		// 二进制数据，value
		byte[] f = readToByte(file);
		String filepath = byteUploadFile(f);
		System.out.println(filepath);
//		del("lhBc6-sfZk6tzbBp8BCgABUpOHEU");

	}

}
